//
//  PersonalInfoViewController.m
//  Utils
//
//  Created by enghou on 2018/5/1.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

#import "PersonalInfoViewController.h"
#import <TZImagePickerController.h>
#import "NetWorkerManager.h"
#import "NumberChecker.h"
#import "LocalPersistent.h"
#import "FileItemModel.h"
@interface PersonalInfoViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *headImage;
@property (weak, nonatomic) IBOutlet UITextField *nameField;
@property (weak, nonatomic) IBOutlet UITextField *phoneField;

@end

@implementation PersonalInfoViewController
- (IBAction)submitAction:(id)sender {
//    /mobile/comprehensiveGovernance/userMobileManage/updateUserInfo.action
    if ([self.nameField.text length] == 0) {
        [self alertSome:@"请输入姓名!"];
        return;
    }
    if (![NumberChecker validPhone:self.phoneField.text]) {
        [self alertSome:@"请输入正确的手机号!"];
        return;
    }
    
    if (self.headImage.image) {
        NSData *data = UIImageJPEGRepresentation(self.headImage.image, 0.1);
        [self uploadImage:data successBlock:^(NSString *path) {
            [[LocalPersistent sharedInstance]saveContent:[NSString stringWithFormat:@"%@/%@",ServerPath,path] forKey:@"headimage"];
            [self submitWithPath:path];
        }];
    }else{
        [self submitWithPath:nil];
    }
}

-(void)submitWithPath:(NSString *)path{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setObject:self.nameField.text forKey:@"user.name"];
    [param setObject:self.uid forKey:@"user.id"];
    [param setObject:self.phoneField.text forKey:@"user.mobile"];
    if ([path length] > 0) {
        [param setObject:path forKey:@"user.headerUrl"];
    }
    [self beginLoading];
    [[NetWorkerManager sharedInstance]postWithPath:@"/mobile/comprehensiveGovernance/userMobileManage/updateUserInfo.action" parameter:param completeBlock:^(NSDictionary *source) {
        [self endLoading];
        BaseModel *model = [[BaseModel alloc]initWithSourceDict:source];
        if (model.success) {
            [self pop];
        }else{
            [self alertSome:model.msg];
        }
    } failBlock:^(NSInteger code) {
        [self endLoading];
        [self alertSome:kNetWorkFail];
    }];
}

-(void)uploadImage:(NSData *)image successBlock:(void(^)(NSString *))suc{
    [self beginLoading];
    [[NetWorkerManager sharedInstance]uploadImageWithPath:kUploadFilePath parameter:nil files:@[image] fileName:@"file" suffix:@"jpg" completeBlock:^(NSDictionary *source) {
        [self endLoading];
        BaseModel *model = [[BaseModel alloc]initWithSourceDict:source listItemClass:NSClassFromString(@"FileItemModel")];
        if (model.success) {
            FileItemModel *file = model.list[0];
            suc(file.path);
        }
    } failBlock:^(NSInteger code) {
        [self endLoading];
        [self alertSome:kNetWorkFail];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.nameField.text = self.user_name;
    self.phoneField.text = self.user_phone;
    if ([[LocalPersistent sharedInstance]contentForKey:@"headimage"]) {
        [self.headImage sd_setImageWithURL:[[LocalPersistent sharedInstance]contentForKey:@"headimage"] placeholderImage:[UIImage imageNamed:@"icon_empty_head"]];
    }
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        TZImagePickerController *picker = [[TZImagePickerController alloc]initWithMaxImagesCount:1 delegate:nil];
        picker.didFinishPickingPhotosHandle = ^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
            UIImage *image = photos[0];
            self.headImage.image = image;
        };
        [self presentViewController:picker animated:YES completion:nil];
    }
}

@end
